package com.wohuijituan.whcsmerchant.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.bean.BankListBean;

import java.util.List;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/8/23 11:54
 * 版本：V1.0
 * 修改历史：
 */
public class SelectBankCardAdapter extends MyBaseAdapter<BankListBean.DataEntity>{
    //用于图片加载
    private DisplayImageOptions mImageOptions;
    private ImageLoader mImageLoader;
    private int checkedPosition;
    public SelectBankCardAdapter(Context ctx, List<BankListBean.DataEntity> dataList) {
        super(ctx, dataList);
        mImageLoader= ImageLoader.getInstance();
        mImageOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageForEmptyUri(R.drawable.icon_nonghang)
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher)
                .build();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null){
            convertView = View.inflate(ctx, R.layout.item_select_bank_card,null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (position==checkedPosition){
            vh.radiobtnselect.setChecked(true);
        }else {
            vh.radiobtnselect.setChecked(false);
        }
        BankListBean.DataEntity bankCard = dataList.get(position);
        vh.tvbankname.setText(bankCard.getB_Name());//显示银行名称
        String cardNumber = bankCard.getSMB_Number();
        String end = cardNumber.substring(cardNumber.length()-4);//截取字符串后四位
        vh.tvbankcardnumber.setText("尾号"+end);
        mImageLoader.displayImage(bankCard.getB_Icon(), vh.ivbankicon, mImageOptions);//给商品设值图片
        return convertView;
    }

    /**
     * 设置选择位置,并进行通知刷新界面
     * @param checkedPosition
     */
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }
    public class ViewHolder {
        public final ImageView ivbankicon;
        public final TextView tvbankname;
        public final TextView tvbankcardnumber;
        public final RadioButton radiobtnselect;
        public final LinearLayout llselectbank;
        public final View root;

        public ViewHolder(View root) {
            ivbankicon = (ImageView) root.findViewById(R.id.iv_bank_icon);
            tvbankname = (TextView) root.findViewById(R.id.tv_bank_name);
            tvbankcardnumber = (TextView) root.findViewById(R.id.tv_bank_card_number);
            radiobtnselect = (RadioButton) root.findViewById(R.id.radio_btn_select);
            llselectbank = (LinearLayout) root.findViewById(R.id.ll_select_bank);
            this.root = root;
        }
    }
}
